var yhsx = {
    setBtnStatus:function () {
        var row = $('#grid_yhsx').datagrid('getSelected');
        if (row && row.id&& row.zt) {
            if(row.zt == 0){//新建
                $("#btn_yhsx_mod").show();
                $("#btn_yhsx_del").show();
                $("#btn_yhsx_confirm").show();
                $("#btn_yhsx_yhhk").hide();
                $("#btn_yhsx_qxqr").hide();
            } else if(row.zt == 1){//确认
                $("#btn_yhsx_mod").hide();
                $("#btn_yhsx_del").hide();
                $("#btn_yhsx_confirm").hide();
                $("#btn_yhsx_yhhk").show();
                $("#btn_yhsx_qxqr").show();
            } else if(row.zt == 2){//还款中
                $("#btn_yhsx_mod").hide();
                $("#btn_yhsx_del").hide();
                $("#btn_yhsx_confirm").hide();
                $("#btn_yhsx_yhhk").show();
                $("#btn_yhsx_qxqr").hide();
            } else {//3  还款完毕
                $("#btn_yhsx_mod").hide();
                $("#btn_yhsx_del").hide();
                $("#btn_yhsx_confirm").hide();
                $("#btn_yhsx_yhhk").hide();
                $("#btn_yhsx_qxqr").hide();
            }
        } else {//3  未选择
            $("#btn_yhsx_mod").hide();
            $("#btn_yhsx_del").hide();
            $("#btn_yhsx_confirm").hide();
            $("#btn_yhsx_yhhk").hide();
            $("#btn_yhsx_qxqr").hide();
        }
    },
    alwayShowDetail:function (rowData) {
        $('#layout_yhsx').layout('remove', 'center');
        $('#layout_yhsx').layout('add', {
            region:'center',
            split:true,
            href:$.webPath() + '/zjgl/yhsx/yhsx_detail?yhsxId=' + rowData.id
        });
    },
    showDetail:function (rowData) {
        if (rowData && $("#yhsx_detail_id").val() != rowData.id) {
            yhsx.alwayShowDetail(rowData);
        }
    }
}

$(function () {
    var sysDate = $("#yhsx_sysDate").val();
    //表格初始加载
    $('#grid_yhsx').datagrid({
        url:$.webPath() + "/zjgl/yhsx/yhsxList",
        toolbar:"#toolbar_yhsx",
        fitColumns:true,
        singleSelect:true,
        pagination:true,
        pageNumber:1,
        fit:true,
        rownumbers:true,
        showFooter: true,
        striped:true,
        columns:[[
            {field:'jkrq', title:'借款日期', width:20,align:'center'},
            {field:'khh_jc', title:'银行名称', width:20,align:'center'},
            {field:'jkje', title:'借款金额（元）',align:'right',
                formatter:function(value){
                    var sl = formatJe2(value,",");
                    return sl;
                },width:30},
            {field:'yll', title:'月利率‰',align:'right', formatter:function(value){
                var val = formatJeForHtml(value);
                return val;
            },width:20},
            {field:'dqrq', title:'到期日期', width:20,align:'center',
                formatter:function(value){
                var val = formatDate(sysDate,value,5);
                return val;
            }},
            {field:'lxe', title:'利息额（元）',align:'right',
                formatter:function(value){
                var sl = formatJe2(value,",");
                return sl;
            },width:25},
            {field:'ljhk', title:'累计还款（元）',align:'right',
                formatter:function(value){
                    var sl = formatJe2(value,",");
                    return sl;
                },width:20},
            {field:'nbgs_jc', title:'公司名称', width:20,align:'center'},
//            {field:'sxgs_qc', title:'授信公司', width:30,align:'center'},
            {field:'dkf_qc', title:'贷款方', width:30,align:'center'},
            {field:'sxgsejzh_qc', title:'二级账户', width:30,align:'center'},
            {field:"bz" ,title:"备注",width:20,align:'center'},
            {field:'zdr_xm', title:'制单人', width:15,align:'center'},
            {field:"sxlx" ,title:"授信类型",width:20,align:'center',
                formatter:function (value) {
                    if (value == 'yhsx'){
                        return '银行授信';
                    } else if (value == 'wlysx'){
                        return '物流园授信';
                }
                }
            },
            {field:"zt" ,title:"状态",width:10,align:'center',
                formatter:function (value) {
                    if (value == '0'){
                        return '未还款';
                    } else if (value == '1'){
                        return '已确认';
                    } else if (value == '2'){
                        return '还款中';
                    }else if (value == '3'){
                        return '还款完毕';
                    }
                }
            }
        ]],
        onLoadSuccess:function(){
            $(this).datagrid('selectRow', 0);
            yhsx.setBtnStatus();
            $(".datagrid-htable .datagrid-cell").css('text-align', 'center');
        },
        onClickRow:function(){
            yhsx.setBtnStatus();
        },
        onSelect:function (rowIndex, rowData) {
            yhsx.showDetail(rowData);
        }
    });

    //**********按钮触发事件**********
    //新增按钮
    $("#btn_yhsx_add").click(function () {
        yhsx_add('新增银行授信', 'add_yhsx');
    });
    //新增按钮
    $("#btn_yhsx_wlysx").click(function () {
        yhsx_wlysx('新增物流园授信', 'add_wlysx');
    });
    //修改按钮
    $("#btn_yhsx_mod").click(function () {
        yhsx_modify('修改银行授信', 'update_yhsx');
    });
    //删除按钮
    $("#btn_yhsx_del").click(function () {
        yhsx_delete();
    });
    //还款按钮
    $("#btn_yhsx_yhhk").click(function () {
        yhsx_yhhk('银行还款', 'yhsx_hk');
    });
    //查询按钮
    $("#btn_yhsx_search").click(function () {
        yhsx_search();
    });

    //确认按钮
    $("#btn_yhsx_confirm").click(function () {
        yhsx_confirm();
    });

    //取消
    $("#btn_yhsx_qxqr").click(function () {
        yhsx_qxqr();
    });


    //双击事件
    $("#grid_yhsx").datagrid({
        onDblClickCell:function (index, field) {
            if (field == "jkrq") {
                $("#btn_yhsx_mod:visible").click();
            }
        }
    });
    //加载公司机构下拉框
    $("#gsmcId").combotree({
        loadFilter: function (rows) {
            return convert(rows);
        }
    });
});

//新增银行授信
function yhsx_add(title, action) {
    var href = $.webPath() + "/zjgl/yhsx/yhsx_content";
    var actionUrl = $.webPath() + "/zjgl/yhsx/"+action;
    yhsx_showDialog(title, href, actionUrl);
}
//新增物流园授信
function yhsx_wlysx(title, action) {
    var href = $.webPath() + "/zjgl/yhsx/wlysx_content";
    var actionUrl = $.webPath() + "/zjgl/yhsx/"+action;
    wlysx_showDialog(title, href, actionUrl);
}
//修改银行授信
function yhsx_modify(title, action) {
    var record = $("#grid_yhsx").datagrid("getSelected");
    if (record == null) {
        $.messager.alert("提示", "请选择要更新的记录!", "warning");
        return;
    }
    if(record.sxlx=='yhsx'){
        var href = $.webPath() + "/zjgl/yhsx/yhsx_content?yhsxId=" + record.id;
        var actionUrl = $.webPath() + "/zjgl/yhsx/update_yhsx";
        yhsx_showDialog('修改银行授信', href, actionUrl);
    }
    if(record.sxlx=='wlysx'){
        var href = $.webPath() + "/zjgl/yhsx/wlysx_content?yhsxId=" + record.id;
        var actionUrl = $.webPath() + "/zjgl/yhsx/update_wlysx";
        wlysx_showDialog('修改物流园授信', href, actionUrl);
    }


}
function wlysx_modify(title, action) {
    var record = $("#grid_yhsx").datagrid("getSelected");
    if (record == null) {
        $.messager.alert("提示", "请选择要更新的记录!", "warning");
        return;
    }
    var href = $.webPath() + "/zjgl/yhsx/wlysx_content?yhsxId=" + record.id;
    var actionUrl = $.webPath() + "/zjgl/yhsx/" + action;
    yhsx_showDialog(title, href, actionUrl);
}
//弹出银行授信窗口
function yhsx_showDialog(title, href, actionUrl) {
    var win = $.window({
        title:title,
        width:620,
        height:360,
        cache:false,
        url:href,
        modal:true,
        buttons:[
            {
                text:'保存',
                handler:function () {
                    $(win).mask();
                    $('#form_yhsx').form('submit', {
                        url:actionUrl,
                        onSubmit:function () {
                            $("[name='yhsx.zt']").val("0");
                            var isValid = $(this).form('validate');
                            if($("[name='yhsx.jkje']").val() == 0.00 || $("[name='yhsx.jkje']").val() < 0.00){
                                $.messager.alert("提示", "借款金额必须大于0.00元!", "waring");
                                $(win).mask('hide');
                                return false;
                            }
                            if (!isValid) {
                                $(win).mask('hide');
                                return false;
                            }
                            return isValid;
                        },
                        success:function (data) {
                            $(win).mask('hide');
                            var json = $.parseJSON(data);
                            if (json.statusCode == 200) {
                                $(win).window('close');
                                $('#grid_yhsx').datagrid('reload');
                                $.messager.show({
                                    title:'提示',
                                    msg:json.message,
                                    timeout:3000,
                                    showType:'slide'
                                });
                            } else {
                                $.messager.alert('提示', json.message, 'error');
                            }
                        }
                    });
                }
            }
        ]
    });
}

//弹出银行授信窗口
function wlysx_showDialog(title, href, actionUrl) {
    var win = $.window({
        title:title,
        width:620,
        height:360,
        cache:false,
        url:href,
        modal:true,
        buttons:[
            {
                text:'保存',
                handler:function () {
                    $(win).mask();
                    $('#form_yhsx').form('submit', {
                        url:actionUrl,
                        onSubmit:function () {
                            $("[name='yhsx.zt']").val("0");
                            var isValid = $(this).form('validate');
                            if($("[name='yhsx.jkje']").val() == 0.00 || $("[name='yhsx.jkje']").val() < 0.00){
                                $.messager.alert("提示", "借款金额必须大于0.00元!", "waring");
                                $(win).mask('hide');
                                return false;
                            }
                            if (!isValid) {
                                $(win).mask('hide');
                                return false;
                            }
                            return isValid;
                        },
                        success:function (data) {
                            $(win).mask('hide');
                            var json = $.parseJSON(data);
                            if (json.statusCode == 200) {
                                $(win).window('close');
                                $('#grid_yhsx').datagrid('reload');
                                $.messager.show({
                                    title:'提示',
                                    msg:json.message,
                                    timeout:3000,
                                    showType:'slide'
                                });
                            } else {
                                $.messager.alert('提示', json.message, 'error');
                            }
                        }
                    });
                }
            }
        ]
    });
}

//删除银行授信
function yhsx_delete() {
    var row = $('#grid_yhsx').datagrid('getSelected');
    if (row) {
        $.messager.confirm('提示', '是否删除该银行授信？', function (r) {
            if (r) {
                var index = $('#grid_yhsx').datagrid('getRowIndex', row);
                $.ajax({
                    url: $.webPath() + "/zjgl/yhsx/delete_yhsx",
                    dataType: 'json',
                    type: 'POST',
                    data: {yhsxId:row.id},
                    beforeSend: function () {
                        $("#layout_yhsx").mask();
                    },
                    success:function(data){
                        $("#layout_yhsx").mask("hide");
                        if(data.statusCode == 200){
                            $('#grid_yhsx').datagrid('deleteRow', index);
                            $('#grid_yhsx').datagrid('reload');
                            $.messager.show({
                                title:'提示',
                                msg:data.message,
                                timeout:3000,
                                showType:'slide'
                            });
                        }else{
                            $.messager.alert("提示", data.message, "error");
                        }
                    }
                });
            }
        });
    } else {
        $.messager.alert("提示", "请选择要删除的银行授信！", "warning");
    }
}

//取消确认
function yhsx_qxqr(){
    var row = $('#grid_yhsx').datagrid('getSelected');
    if (row) {
        $.messager.confirm('提示', '是否取消该银行授信确认？', function (r) {
            if (r) {
                var index = $('#grid_yhsx').datagrid('getRowIndex', row);
                $.ajax({
                    url: $.webPath() + "/zjgl/yhsx/qxqr_yhsx",
                    dataType: 'json',
                    type: 'POST',
                    data: {yhsxId:row.id},
                    beforeSend: function () {
                        $("#layout_yhsx").mask();
                    },
                    success:function(data){
                        $("#layout_yhsx").mask("hide");
                        if(data.statusCode == 200){
                            $('#grid_yhsx').datagrid('reload');
                            $.messager.show({
                                title:'提示',
                                msg:data.message,
                                timeout:3000,
                                showType:'slide'
                            });
                        }else{
                            $.messager.alert("提示", data.message, "error");
                        }
                    }
                });
            }
        });
    } else {
        $.messager.alert("提示", "请选择要取消确认的银行授信！", "warning");
    }
}

//查询银行授信
function yhsx_search() {
    var gsId =  $("#gsmcId").combotree('getValue') == null ? null :  $("#gsmcId").combotree('getValue');
    var gsMc =  $("#gsmcId").combotree('getText') == null ? null :  $("#gsmcId").combotree('getText');
    var jkrqStart = $('#jkrq_start').datebox('getValue');
    var jkrqEnd = $('#jkrq_end').datebox('getValue');
    var zt = $('#yhsx_zt').combobox('getValue') == null ? null : $('#yhsx_zt').combobox('getValue');
    $('#grid_yhsx').datagrid('load',
        {
            gsId: gsId ,
            gsMc: gsMc ,
            jkrqStart:jkrqStart,
            jkrqEnd:jkrqEnd,
            yhMc:$("#yhsx_yh_mc").val(),
            zt:zt
        }
    );
}

//银行授信还款
function yhsx_yhhk(title, action) {
    var record = $("#grid_yhsx").datagrid("getSelected");
    if (record == null) {
        $.messager.alert("提示", "请选择要还款的记录!", "warning");
        return;
    }
    var href = $.webPath() + "/zjgl/yhsx/yhsx_yhhk?yhsxId=" + record.id;
    var actionUrl = $.webPath() + "/zjgl/yhsx/" + action;
    yhsx_hkDialog(title, href, actionUrl);
}

//银行授信还款窗口
function yhsx_hkDialog(title, href, actionUrl){
    var win = $.window({
        title:title,
        width:620,
        height:360,
        cache:false,
        url:href,
        modal:true,
        buttons:[
            {
                text:'保存',
                handler:function () {
                    $(win).mask();
                    $('#form_yhhk_yhsx').form('submit', {
                        url:actionUrl,
                        onSubmit:function () {
                            var isValid = $(this).form('validate');
                            if($("[name='yhsxmx.hkje']").val() == 0.00 || $("[name='yhsxmx.hkje']").val() < 0.00){
                                $.messager.alert("提示", "还款金额必须大于0.00元!", "waring");
                                $(win).mask('hide');
                                return false;
                            }
                            if (!isValid) {
                                $(win).mask('hide');
                                return false;
                            }
                            return isValid;
                        },
                        success:function (data) {
                            $(win).mask('hide');
                            var json = $.parseJSON(data);
                            if (json.statusCode == 200) {
                                $(win).window('close');
                                $('#grid_yhsx').datagrid('reload');
                                $.messager.show({
                                    title:'提示',
                                    msg:json.message,
                                    timeout:3000,
                                    showType:'slide'
                                });
                            } else
                                $.messager.alert('提示', json.message, 'error');
                        }
                    });
                }
            }
        ]
    });
}

//确认银行授信
function yhsx_confirm() {
    var row = $('#grid_yhsx').datagrid('getSelected');
    if (row) {
        $.messager.confirm('提示', '是否对该银行授信发起流程？', function (r) {
            if (r) {
                $.ajax({
                    url: $.webPath() + "/zjgl/yhsx/yhsx_confirm",
                    dataType: 'json',
                    type: 'POST',
                    data: {yhsxId: row.id, zt: 1},
                    success: function (data) {
                        if (data.statusCode == 200) {
                            $('#grid_yhsx').datagrid('reload');
                            $.messager.show({
                                title: '提示',
                                msg: data.message,
                                timeout: 3000,
                                showType: 'slide'
                            });
                            yhsx.alwayShowDetail($('#grid_yhsx').datagrid("getSelected"));
                        } else {
                            $.messager.alert("提示", data.message, "error");
                        }
                    }
                });
            }
        });
    } else {
        $.messager.alert("提示", "请选择要发起流程的银行授信！", "warning");
    }
}